package com.yinhe.music.yhmusic.utils;

import android.app.ActivityManager;
import android.app.Notification;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import android.text.format.DateUtils;
import android.widget.RemoteViews;

import com.yinhe.music.yhmusic.R;
import com.yinhe.music.yhmusic.constants.Actions;
import com.yinhe.music.yhmusic.constants.Extras;
import com.yinhe.music.yhmusic.model.Music;
import com.yinhe.music.yhmusic.music.PlayingActivity;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;


/**
 * Created by hzwangchenyan on 2016/3/22.
 */
public class SystemUtils implements Actions {

    /**
     * 判断是否有Activity在运行
     */
    public static boolean isStackResumed(Context context) {
        ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> runningTaskInfos = manager.getRunningTasks(1);
        ActivityManager.RunningTaskInfo runningTaskInfo = runningTaskInfos.get(0);
        return runningTaskInfo.numActivities > 1;
    }

    /**
     * 判断Service是否在运行
     */
    public static boolean isServiceRunning(Context context, Class<?> serviceClass) {
        ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
            if (serviceClass.getName().equals(service.service.getClassName())) {
                return true;
            }
        }
        return false;
    }

    public static Notification createNotification(boolean isPlaying, Context context, Music music) {
        String title = music.getTitle();
        String subtitle = FileUtils.getArtistAndAlbum(music.getArtist(), music.getAlbumName());
        RemoteViews mRemoteViews = new RemoteViews(context.getPackageName(), R.layout.notification_layout);

        mRemoteViews.setImageViewResource(R.id.image, R.mipmap.ic_logo);


        mRemoteViews.setTextViewText(R.id.name, title);
        mRemoteViews.setTextViewText(R.id.singer, subtitle);

        Intent pauseIntent = new Intent(ACTION_MEDIA_PLAY_PAUSE);
        PendingIntent pausePIntent = PendingIntent.getBroadcast(context, 0, pauseIntent, 0);
        mRemoteViews.setImageViewResource(R.id.play, isPlaying ? R.drawable.ic_notice_stop : R.drawable.ic_notice_play);
        mRemoteViews.setOnClickPendingIntent(R.id.play, pausePIntent);

        Intent nextIntent = new Intent(ACTION_MEDIA_NEXT);
        PendingIntent nextPIntent = PendingIntent.getBroadcast(context, 0, nextIntent, 0);
        mRemoteViews.setOnClickPendingIntent(R.id.next, nextPIntent);

        Intent prevIntent = new Intent(ACTION_MEDIA_NEXT);
        PendingIntent prevPIntent = PendingIntent.getBroadcast(context, 0, prevIntent, 0);
        mRemoteViews.setOnClickPendingIntent(R.id.before, prevPIntent);

//         Intent cancelIntent = new Intent(ACTION_CANCEL);
//        PendingIntent cancelPIntent = PendingIntent.getBroadcast(context, 0 , cancelIntent, 0);
//        mRemoteViews.setOnClickPendingIntent(R.id.cancel,cancelPIntent);

        Intent intent = new Intent(context, PlayingActivity.class);
        intent.putExtra(Extras.FROM_NOTIFICATION, true);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        builder.setContentIntent(pendingIntent);
        builder.setContent(mRemoteViews);
        builder.setSmallIcon(R.drawable.ic_notification);
        builder.setCustomBigContentView(mRemoteViews);
        // 【适配Android8.0】设置Notification的Channel_ID,否则不能正常显示
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            builder.setChannelId("notification_id");
        }
        return builder.build();
    }

    public static String formatTime(String pattern, long milli) {
        int m = (int) (milli / DateUtils.MINUTE_IN_MILLIS);
        int s = (int) ((milli / DateUtils.SECOND_IN_MILLIS) % 60);
        String mm = String.format(Locale.getDefault(), "%02d", m);
        String ss = String.format(Locale.getDefault(), "%02d", s);
        return pattern.replace("mm", mm).replace("ss", ss);
    }

    public static String formatDate(String stringDate) {
        //日期输出格式
        String targetFormat = "yyyy-MM-dd HH:mm:ss";
        //日期原始格式
        String origFormat = "EEE,dd MMM yyyy HH:mm:ss 'GMT'";

        System.out.println(stringDate);
        DateFormat format = new SimpleDateFormat(origFormat, Locale.CHINA);
        try {
            Date date = format.parse(stringDate);
            format = new SimpleDateFormat(targetFormat);
            String dateString = format.format(date);
            System.out.println(dateString);
            return dateString;
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }

    }

    public static String formatPlay(String play) {
        if (Integer.parseInt(play) < 10000) {
            return play;
        } else if (Integer.parseInt(play) > 10000) {
            return (Integer.parseInt(play) / 10000) + "万";
        } else {
            return null;
        }
    }
}
